dojo.provide("rptp.app");

dojo.require("dojo.lang.declare");

dojo.require("lotex.browser");
dojo.require("lotex.io");
dojo.require("lotex.uri");
dojo.require("lotex.widget.Toaster");
dojo.require("rptp.rpt");

//lotexConfig.baseurl =

rptp.app = {
    messageTopic : "appMessageTopic",
    openEditWindow : function (docID, revision, invitation, prefix) {
        if (docID == 0) {
            this.message("docID == 0");
            // rce("openEditWindow", "docID == 0");
            // asyncPOST("command=allocID", //flip the state
            //  prefix + "UserMiscCommands",
            //   function (req, timedOut) { if (req == null || timedOut) return;  if (req.responseText != "") openEditWindow(req.responseText, revision, invitation, ''); }, "openNewEditWindow");
            //
            // return false; //Let the async deal with it.
        }

        var url = rptp.rpt.getEditWindowURL(docID, revision, invitation, prefix);
        this.message(url);
        // Make sure docID is really just the docID!
        if (docID.indexOf('&') != -1) {
            docID = docID.substring(0, docID.indexOf('&'));
        }
        return lotex.browser.popupWindow(url, "Editor_" + docID, false);
    },

    createNewRpt: function() {
        var self = this;
        lotex.io.bind({
            preventCache: false,
            useCache: false,
            url: [rptp.rpt.getNewRptURL(), "rptd!new.html.json"],
            error: function(type, data, evt){
                self.message("不能创建新报表，服务器错误！");
            },
            load: function(type, data, evt) {
                var rptId = data["message"];
                var url = rptp.rpt.getEditWindowURL(rptId);
                dojo.event.topic.publish("rptListMessageTopic", "reflash");
                try {
                    self.openEditWindow(rptId);
                } catch (e) {alert("弹出窗口时出现错误！");}
            },
            mimetype: "text/json"
        });
        return false;
    },

    getEditWindowURL: function(docID, revision, invitation, prefix) {
        return "/rptps3/rptd!edit.do?definitionId=" + docID;
    },

    message : function (message) {
        dojo.event.topic.publish(this.messageTopic, message);
    }
};

function GetMashedURL(page, params)
{
    var paramString = "";
    if (params != "")
        paramString = "?" + params;

  if (location.host.indexOf("localhost") > -1)
  {
        if (location.host.indexOf("Prefactor") > -1)
            return "http://localhost:8180/Prefactor/" + page + paramString;
        else
            return "http://localhost:8180/Docster/" + page + paramString;
    }
    else
        return "http://" + location.host + "/" + page + paramString;
}

function getHomeURL()
{
    return GetMashedURL("/", "");
}

app = rptp.app;